//
//  LKWorkOrderListTableViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/4/16.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKWorkOrderListTableViewController.h"
#import "LKWorkOrderUpdateViewController.h"
#import "LKWorkOrderListCell.h"
#import "LKWorkOrderDetailViewController.h"
#import "LKOrderModel.h"
@interface LKWorkOrderListTableViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)LKBaseTableview *tableView;
@property(nonatomic,assign)NSInteger PageIndex;
@property(nonatomic,assign)NSInteger MaxResultCount;
@property(nonatomic,strong)NSMutableArray *dataListArray;

@end

@implementation LKWorkOrderListTableViewController


-(LKBaseTableview *)tableView{
    if (!_tableView) {
        _tableView = [LKBaseTableview setTableviewWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - (NAVIGATION_BAR_HEIGHT + 45 + TAB_BAR_HEIGHT)) style:UITableViewStyleGrouped registercellClasss:@[@"LKWorkOrderListCell"]];
        [self.view addSubview:_tableView];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
    }
    return _tableView;
}
/**
 头部下拉刷新
 */
-(void)headerReloadData{
    self.PageIndex = 1;
    NSString *pageIndexStr = [NSString stringWithFormat:@"%ld",self.PageIndex];
    NSString *maxRsult = [NSString stringWithFormat:@"%ld",self.MaxResultCount];
    [self getDataListWithParam:@{@"PageIndex":pageIndexStr,@"MaxResultCount":maxRsult}];
}

/**
 尾部上拉加载更多
 */
-(void)footerReloadData{
    self.PageIndex++;
    NSString *pageIndexStr = [NSString stringWithFormat:@"%ld",self.PageIndex];
    NSString *maxRsult = [NSString stringWithFormat:@"%ld",self.MaxResultCount];
        [self getDataListWithParam:@{@"PageIndex":pageIndexStr,@"MaxResultCount":maxRsult}];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.estimatedRowHeight = 200;
    self.PageIndex = 1;
    self.MaxResultCount = 10;
    NSString *pageIndexStr = [NSString stringWithFormat:@"%ld",self.PageIndex];
    NSString *maxRsult = [NSString stringWithFormat:@"%ld",self.MaxResultCount];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerReloadData)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerReloadData)];
    [self.tableView.mj_header beginRefreshing];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noticeheaderReloadData) name:@"AddOrderUpateNotice" object:nil];
//    [self getDataListWithParam:@{@"PageIndex":pageIndexStr,@"MaxResultCount":maxRsult}];
//    [self.tableView reloadData];
    // Do any additional setup after loading the view.
}
-(void)noticeheaderReloadData{
//    [self getAccountDetailInformation];
    [self headerReloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataListArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LKWorkOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKWorkOrderListCell"];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [cell.labelsView.tableview addGestureRecognizer:tap];
    WS(weakSelf);
    [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        if (weakSelf.delegate) {
            LKWorkOrderDetailViewController *vc = [[LKWorkOrderDetailViewController alloc] init];
            vc.model = weakSelf.dataListArray[indexPath.section];
            [weakSelf.delegate orderPushVc:vc];
        }
    }];
    [cell.editBtn addTarget:self action:@selector(editOrderInformation:) forControlEvents:UIControlEventTouchUpInside];
    cell.editBtn.tag = 10000 + indexPath.section;
    [cell.deleteBtn addTarget:self action:@selector(deletCustomerACtion:) forControlEvents:UIControlEventTouchUpInside];
    cell.deleteBtn.tag = 100000 + indexPath.section;
    cell.model = self.dataListArray[indexPath.section];
    return cell;
}

/**
 编辑订单详情

 */
-(void)editOrderInformation:(UIButton *)btn{
    if (self.delegate) {
        LKWorkOrderUpdateViewController *vc = [[LKWorkOrderUpdateViewController alloc] init];
        vc.theme = [NSString stringWithFormat:@"%@订单",btn.titleLabel.text];
        LKOrderModel *model = self.dataListArray[btn.tag - 10000];
        vc.updateId = model.orderId;
        vc.model = [LKCustomerModel modelWithDictionary:@{}];
        vc.model.customerCode = model.customerCode;
        vc.model.customerId = model.customerId;
        [self.delegate orderPushVc:vc];
    }
}
-(void)deletCustomerACtion:(UIButton *)btn{
    LKOrderModel *model = self.dataListArray[btn.tag - 100000];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确认要删除?" message:model.customerName preferredStyle:UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self deleteCustomerWithModel:model];
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 310;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0000000000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 13;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = cellHeaderBgColor;
    return view;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate) {
        LKWorkOrderDetailViewController *vc = [[LKWorkOrderDetailViewController alloc] init];
        vc.model = self.dataListArray[indexPath.section];
        [self.delegate orderPushVc:vc];
    }
}
/**
 删除客户
 */
-(void)deleteCustomerWithModel:(LKOrderModel *)model{
    
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:deleterOrderUrl header:[LKUserInforTool header] parameters:@{@"Id":model.orderId} progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        
        [self.dataListArray removeObject:model];
        [self.tableView reloadData];
//        [self getAccountDetailInformation];
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
/**
 获取订单列表
 */
-(void)getDataListWithParam:(NSDictionary *)param{
    
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:getOrderListUrl header:[LKUserInforTool header] parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (self.PageIndex == 1) {
            [self.dataListArray removeAllObjects];
        }
        NSDictionary *resultDic = responseObject[@"result"];
        NSArray *dataArra = resultDic[@"item"];
        NSString *count = responseObject[@"result"][@"count"];
        for (NSDictionary *dic in dataArra) {
            LKOrderModel *model = [LKOrderModel modelWithDictionary:dic];
            [self.dataListArray addObject:model];
        }
        if ([count integerValue] == self.dataListArray.count) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        if (self.dataListArray.count == 0) {
            self.tableView.mj_footer.hidden = YES;
        }
        [self.tableView reloadData];
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}

-(NSMutableArray *)dataListArray{
    if (!_dataListArray) {
        _dataListArray = [[NSMutableArray alloc] init];
    }
    return _dataListArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
